﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CooStationsToolsNew.Model;
using System.IO;

namespace CooStationsToolsNew
{
    /// <summary>
    /// WorkLeftAreaUC.xaml 的交互逻辑
    /// </summary>
    public partial class WorkLeftAreaUC : UserControl
    {
        Window win;
        public static PageListUC pluc = null;
        public static ObjectTreeViewUC theOtv=null;
        public static List<string[]> treelist = new List<string[]>();
        ModelTools model=new ModelTools();
        public WorkLeftAreaUC(Window _win)
        {
            InitializeComponent();
            win = _win;
        }

        private void tabItem2_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void leftbtn_hidden_Click(object sender, RoutedEventArgs e)
        {

        }

        private void left2btn_hidden_Click(object sender, RoutedEventArgs e)
        {

        }

        private void tabItem3_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void tabControl2_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        /// <summary>
        /// 向指定节点添加对象
        /// </summary>
        /// <param name="tv">指定节点</param>
        /// <param name="obj">对象</param>
        public void AddTreeView(TreeViewItem tv, UIElement obj)
        {
            TreeViewItem item = new TreeViewItem();
            item.Selected += (o, e) =>
            {
                TreeViewItem citem = o as TreeViewItem;
                citem.BorderBrush = new SolidColorBrush(Color.FromRgb(20, 226, 226));
            };
            item.Unselected += (o, e) =>
            {
                TreeViewItem citem = o as TreeViewItem;
                citem.BorderBrush = null;
            };
            item.AllowDrop = true;
            item.Focusable = false;
            item.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(item_PreviewMouseLeftButtonDown);
            item.Expanded += new RoutedEventHandler(item_Expanded);
            item.Collapsed += new RoutedEventHandler(item_Collapsed);
            item.PreviewDrop += new System.Windows.DragEventHandler(item_PreviewDrop);
            item.MouseLeftButtonDown += new MouseButtonEventHandler(item_MouseLeftButtonDown);
            item.Tag = obj;
            ObjectTreeViewUC otv = null;
            if (obj.GetType() == typeof(Canvas))
            {
                otv = new ObjectTreeViewUC("div", obj.Uid);
            }
            else if (obj.GetType() == typeof(Image))
            {
                otv = new ObjectTreeViewUC("img", obj.Uid);
            }
            else if (obj.GetType() == typeof(Rectangle))
            {
                otv = new ObjectTreeViewUC("rect", obj.Uid);
            }
            else if (obj.GetType() == typeof(TextBlock))
            {
                otv = new ObjectTreeViewUC("txt", obj.Uid);
            }
            item.Header = otv;//canvas.Children[i].Uid;
            item.Name = obj.Uid;
            System.Windows.Data.Binding bdotv = new System.Windows.Data.Binding();
            bdotv.Source = item;
            bdotv.Mode = BindingMode.TwoWay;
            bdotv.StringFormat = "F0";
            bdotv.Path = new PropertyPath("Name");
            otv.txtb.SetBinding(TextBlock.TextProperty, bdotv);
            tv.Items.Add(item);
            tv.IsExpanded = true;
        }

        /// <summary>
        /// 绑定TreeView对象管理器
        /// </summary>
        /// <param name="canvas">需要绑定UI</param>
        public void BindingTreeView(Canvas canvas)
        {
            ObjectTreeViewUC.theTreeView = treeView1;
            ObjectTreeViewUC otv = null;
            treeView1.Items.Clear();
            TreeViewItem itemTop = new TreeViewItem();
            //itemTop.Style = FindResource("SimpleTreeViewItem") as Style;
            itemTop.AllowDrop = true;
            itemTop.Focusable = false;
            itemTop.Selected += (o, e) =>
            {
                TreeViewItem citem = o as TreeViewItem;
                citem.BorderBrush = new SolidColorBrush(Color.FromRgb(20, 226, 226));
            };
            itemTop.Unselected += (o, e) =>
            {
                TreeViewItem citem = o as TreeViewItem;
                citem.BorderBrush = null;
            };
                itemTop.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(item_PreviewMouseLeftButtonDown);
                itemTop.Expanded += new RoutedEventHandler(item_Expanded);
                itemTop.Collapsed += new RoutedEventHandler(item_Collapsed);
                itemTop.PreviewDrop += new System.Windows.DragEventHandler(item_PreviewDrop);
                // itemTop.MouseLeftButtonDown += new MouseButtonEventHandler(item_MouseLeftButtonDown);
                otv = new ObjectTreeViewUC("page", App.pageName);
                itemTop.Tag = "page";
                itemTop.Header = otv;
                otv.txtb.Text = App.pageName;
                if (treelist.Count == 0)
                {
                    string[] str = { "page", "true" };
                    treelist.Add(str);
                }
                for (int f = 0; f < treelist.Count; f++)
                {
                    if ((treelist[f])[0] == "page")
                    {
                        if ((treelist[f])[1] == "true")
                        {
                            itemTop.IsExpanded = true;
                        }
                        else if ((treelist[f])[1] == "false")
                        {
                            itemTop.IsExpanded = false;
                        }
                    }
                }
                // itemTop.IsExpanded = true;
                for (int i = 0; i < canvas.Children.Count; i++)
                {
                    if (canvas.Children[i].Uid != "")
                    {

                        TreeViewItem item = new TreeViewItem();
                        item.AllowDrop = true;
                        item.Focusable = false;
                       item.Style = FindResource("SimpleTreeViewItem") as Style;
                        item.Selected += (o, e) =>
                        {
                            //TreeViewItem citem = o as TreeViewItem;
                           // citem.Background = new SolidColorBrush(Color.FromRgb(20, 226, 226));
                        };
                        item.Unselected += (o, e) =>
                        {
                            //TreeViewItem citem = o as TreeViewItem;
                            //citem.Background = null;
                        };
                        //item.Style = FindResource("SimpleTreeViewItem") as Style;
                        item.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(item_PreviewMouseLeftButtonDown);
                        item.Expanded += new RoutedEventHandler(item_Expanded);
                        item.Collapsed += new RoutedEventHandler(item_Collapsed);
                        item.PreviewDrop += new System.Windows.DragEventHandler(item_PreviewDrop);
                        item.MouseLeftButtonDown += new MouseButtonEventHandler(item_MouseLeftButtonDown);
                        item.Tag = canvas.Children[i];

                        //ObjectTreeViewUC otv = null;
                        if (canvas.Children[i].GetType() == typeof(Canvas))
                        {
                            otv = new ObjectTreeViewUC("div", canvas.Children[i].Uid);
                            if (canvas.Children[i].GetType() == typeof(Canvas))
                            {

                                Canvas cannew = canvas.Children[i] as Canvas;
                                if (cannew.Children.Count > 0)
                                {
                                    bool flag = false;
                                    for (int j = 0; j < treelist.Count; j++)
                                    {
                                        if ((treelist[j])[0] == canvas.Children[i].Uid)
                                        {
                                            flag = true;
                                            break;
                                        }
                                        else
                                        {

                                        }
                                    }
                                    if (flag == false)
                                    {
                                        string[] str = { canvas.Children[i].Uid, "true" };
                                        treelist.Add(str);
                                    }
                                    for (int k = 0; k < treelist.Count; k++)
                                    {
                                        if ((treelist[k])[0] == canvas.Children[i].Uid)
                                        {
                                            if ((treelist[k])[1] == "true")
                                            {
                                                item.IsExpanded = true;
                                            }
                                            else if ((treelist[k])[1] == "false")
                                            {
                                                item.IsExpanded = false;
                                            }
                                        }
                                    }
                                    BindingTreeView(item, cannew);
                                }
                            }
                        }
                        else if (canvas.Children[i].GetType() == typeof(Image))
                        {
                            otv = new ObjectTreeViewUC("img", canvas.Children[i].Uid);
                        }
                        else if (canvas.Children[i].GetType() == typeof(Rectangle))
                        {
                            otv = new ObjectTreeViewUC("rect", canvas.Children[i].Uid);
                        }
                        else if (canvas.Children[i].GetType() == typeof(TextBlock))
                        {
                            otv = new ObjectTreeViewUC("txt", canvas.Children[i].Uid);
                        }
                        item.Header = otv;//canvas.Children[i].Uid;
                        // item.IsExpanded = true;
                        item.Name = canvas.Children[i].Uid;// item.Items.Add("*");

                        System.Windows.Data.Binding bdotv = new System.Windows.Data.Binding();
                        bdotv.Source = item;
                        bdotv.Mode = BindingMode.TwoWay;
                        bdotv.StringFormat = "F0";
                        bdotv.Path = new PropertyPath("Name");
                        otv.txtb.SetBinding(TextBlock.TextProperty, bdotv);

                        itemTop.Items.Add(item);
                    }
                }
                treeView1.Items.Add(itemTop);
           // }
          //  catch { }
        }
        /// <summary>
        /// 绑定TreeView对象管理器
        /// </summary>
        /// <param name="tv">需要绑定TreeView</param>
        /// <param name="canvas">需要绑定的UI</param>
        public void BindingTreeView(TreeViewItem tv, Canvas canvas)
        {
            tv.Items.Clear();
            ObjectTreeViewUC otv = null;
            TreeViewItem item = null;
            for (int i = 0; i < canvas.Children.Count; i++)
            {
                if (canvas.Children[i].Uid != "")
                {
                    item = new TreeViewItem();
                    item.Selected += (o, e) =>
                    {
                        TreeViewItem citem = o as TreeViewItem;
                        citem.BorderBrush = new SolidColorBrush(Color.FromRgb(20, 226, 226));
                    };
                    item.Unselected += (o, e) =>
                    {
                        TreeViewItem citem = o as TreeViewItem;
                        citem.BorderBrush = null;
                    };
                    item.AllowDrop = true;
                    item.Focusable = false;
                    item.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(item_PreviewMouseLeftButtonDown);
                    item.Expanded += new RoutedEventHandler(item_Expanded);
                    item.Collapsed += new RoutedEventHandler(item_Collapsed);
                    item.PreviewDrop += new DragEventHandler(item_PreviewDrop);
                    item.MouseLeftButtonDown += new MouseButtonEventHandler(item_MouseLeftButtonDown);
                    item.Tag = canvas.Children[i];

                    if (canvas.Children[i].GetType() == typeof(Canvas))
                    {
                        otv = new ObjectTreeViewUC("div", canvas.Children[i].Uid);
                        Canvas cannew = canvas.Children[i] as Canvas;
                        if (cannew.Children.Count > 0)
                        {
                            bool flag = false;
                            for (int j = 0; j < treelist.Count; j++)
                            {
                                if ((treelist[j])[0] == canvas.Children[i].Uid)
                                {
                                    flag = true;
                                    break;
                                }
                                else
                                {

                                }
                            }
                            if (flag == false)
                            {
                                string[] str = { canvas.Children[i].Uid, "true" };
                                treelist.Add(str);
                            }
                            for (int k = 0; k < treelist.Count; k++)
                            {
                                if ((treelist[k])[0] == canvas.Children[i].Uid)
                                {
                                    if ((treelist[k])[1] == "true")
                                    {
                                        item.IsExpanded = true;
                                    }
                                    else if ((treelist[k])[1] == "false")
                                    {
                                        item.IsExpanded = false;
                                    }
                                }
                            }
                            BindingTreeView(item, cannew);
                        }
                    }
                    else if (canvas.Children[i].GetType() == typeof(Image))
                    {
                        otv = new ObjectTreeViewUC("img", canvas.Children[i].Uid);
                    }
                    else if (canvas.Children[i].GetType() == typeof(Rectangle))
                    {
                        otv = new ObjectTreeViewUC("rect", canvas.Children[i].Uid);
                    }
                    else if (canvas.Children[i].GetType() == typeof(TextBlock))
                    {
                        otv = new ObjectTreeViewUC("txt", canvas.Children[i].Uid);
                    }

                    item.Header = otv;//canvas.Children[i].Uid;
                    item.Name = canvas.Children[i].Uid;

                    System.Windows.Data.Binding bdotv = new System.Windows.Data.Binding();
                    bdotv.Source = item;
                    bdotv.Mode = BindingMode.TwoWay;
                    bdotv.StringFormat = "F0";
                    bdotv.Path = new PropertyPath("Name");
                    otv.txtb.SetBinding(TextBlock.TextProperty, bdotv);

                    tv.Items.Add(item);
                }
            }
        }

        void item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragDrop.DoDragDrop((TreeViewItem)sender, (TreeViewItem)sender, System.Windows.DragDropEffects.All);        
        }

        /// <summary>
        /// 拖动放下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void item_PreviewDrop(object sender, System.Windows.DragEventArgs e)
        {
            if (e.KeyStates == DragDropKeyStates.ControlKey)
            {
                TreeViewItem tvi = e.Data.GetData(typeof(TreeViewItem)) as TreeViewItem;
                UIElement dragObj = tvi.Tag as UIElement;
                if (tvi != null)
                {
                    TreeViewItem treeV = null;
                    if (e.Source.GetType() == typeof(ObjectTreeViewUC))
                    {
                        treeV = ((ObjectTreeViewUC)e.Source).Parent as TreeViewItem;
                    }
                    else if (e.Source.GetType() == typeof(TreeViewItem))
                    {
                        treeV = e.Source as TreeViewItem;
                    }
                    if (treeV.Tag.ToString() != "page")
                    {

                        UIElement dragEnterObj = treeV.Tag as UIElement;
                        if (((TreeViewItem)sender).Tag == dragEnterObj)
                        {
                            if (dragObj != dragEnterObj)
                            {
                                onDrogObj(dragObj, dragEnterObj);
                            }
                        }
                    }
                    else
                    {
                        onDrogObj(dragObj, WorkMainAreaUC.workcanvas);
                    }
                }
            }
        }

        public void InitPageList()
        {
            for (int i = 0; i < App.Pagelist.Count; i++)
            {
                Model.CfyPage cfypage = App.Pagelist[i] as Model.CfyPage;
                App.cpage = cfypage;
                if (cfypage.PageName == "cfy")
                {
                    Canvas canP = new Canvas();
                    canP.Width = 150.0;
                    canP.Height = 110.0;
                    PageListUC pagelistuc = new PageListUC(cfypage, WorkMainAreaUC.workcanvas, win);
                    pluc = pagelistuc;
                    pagelistuc.Name = cfypage.PageName;
                    pagelistuc.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(pagelistuc_PreviewMouseLeftButtonDown);
                    pagelistuc.SetValue(Canvas.TopProperty, 0.0);
                    pagelistuc.SetValue(Canvas.LeftProperty, 0.0);
                    canP.Children.Add(pagelistuc);
                    pageListSP.Children.Add(canP);


                }
                else
                {
                    Canvas canP = new Canvas();
                    canP.Width = 150.0;
                    canP.Height = 110.0;
                    canP.MouseEnter += new MouseEventHandler(canP_MouseEnter);
                    canP.MouseLeave += new MouseEventHandler(canP_MouseLeave);
                    Image img = new Image();
                    img.Source = new BitmapImage(new Uri("pic/关闭-1.png", UriKind.RelativeOrAbsolute));
                    img.SetValue(System.Windows.Controls.Panel.ZIndexProperty, 10);
                    img.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(img_PreviewMouseLeftButtonDown);
                    img.Width = 15.0;
                    img.Height = 15.0;
                    img.ToolTip = "删除" + cfypage.PageName;
                    img.Visibility = Visibility.Collapsed;
                    img.SetValue(Canvas.TopProperty, 0.0);
                    img.SetValue(Canvas.LeftProperty, 100.0);
                    PageListUC pagelistuc = new PageListUC(cfypage, null, win);
                    pagelistuc.Name = cfypage.PageName;
                    pagelistuc.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(pagelistuc_PreviewMouseLeftButtonDown);
                    pagelistuc.SetValue(Canvas.TopProperty, 0.0);
                    pagelistuc.SetValue(Canvas.LeftProperty, 0.0);
                    img.Tag = canP;
                    canP.Children.Add(pagelistuc);
                    canP.Children.Add(img);
                    pageListSP.Children.Add(canP);

                }
            }
        }

        void img_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Image img = sender as Image;
            Canvas can = img.Tag as Canvas;
            PageListUC pls = can.Children[0] as PageListUC;
            MessageForm mf = new MessageForm("是否删除" + pls.pageName);
            mf.Owner = win;
            if (mf.ShowDialog() == true)
            {
                using (PageTools pt = new PageTools())
                {
                    pt.DeletePage(pls.pageName);
                    StackPanel sp = can.Parent as StackPanel;
                    sp.Children.Remove(can);
                    NewLoad(sp.Children[0]);
                }
            }
        }

        public void NewLoad(UIElement uiEle)
        {
            //Canvas stcan = uiEle as Canvas;
            ////  SaveTheTempList();
            //PageListUC uc = stcan.Children[0] as PageListUC;
            //pluc = uc;
            ////  uc.GetBG();
            //if (ModelTools.isStyle == "image")
            //{
            //    model.SaveToImageData(ModelTools.pessImg, App.dt);
            //}
            //else if (ModelTools.isStyle == "rectangle")
            //{
            //    model.SaveToRectData(ModelTools.pessRect, App.dt);
            //}
            //ModelTools.isStyle = "";
            //ModelTools.acStyle = "";
            ////clicktxt.Text = "";
            ////overtxt.Text = "";
            ////outtxt.Text = "";
            //pageloadCB.SelectedItem = null;
            //clickCB.SelectedItem = null;
            //overCB.SelectedItem = null;
            //outCB.SelectedItem = null;
            //imgA = null;
            //imgAC = null;
            //rectAC = null;
            //rectA = null;
            //divA = null;
            //divAC = null;
            //textA = null;
            //textAC = null;
            //rectPBorder.Visibility = Visibility.Collapsed;
            //imgPBorder.Visibility = Visibility.Collapsed;
            //App.cpage = uc.cfyp;
            //App.pageName = App.cpage.PageName;
            ////打开页面
            //paget.OpenPage();
            ////删除工作区的Canvas
            //DelCanvas();
            //bodySlider.ValueChanged -= new RoutedPropertyChangedEventHandler<double>(bodySlider_ValueChanged);
            //bodySlider.Value = 1;
            ////新加载一个Canvas
            //Canvas thiscan = CreateCanvas();
            //pt.InitTheWork(App.dt, thiscan);
            //canvas = thiscan;

            //WorkAreaBorder.Child = canvas;
            //if (App.cpage.Width != "" && App.cpage.Height != "")
            //{
            //    canvas.Width = double.Parse(App.cpage.Width);
            //    canvas.Height = double.Parse(App.cpage.Height);
            //}
            //bodySlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(bodySlider_ValueChanged);
            //bodySlider.Value = 0.5;
            //winId.Text = App.cpage.PageName;
            //winWidth.Text = App.cpage.Width;
            //winHeight.Text = App.cpage.Height;
            //pageloadtxt.Text = App.cpage.OnloadInit;
            //winPBorder.Visibility = Visibility.Visible;
            //// uc.mirro.Visual = canvas;

            //BindingTreeView(canvas);
            //if (SaveTheTempList())
            //{
            //    pluc.GetBG();
            //}
        }

        void canP_MouseLeave(object sender, MouseEventArgs e)
        {
            Canvas can = sender as Canvas;
            Image img = can.Children[1] as Image;
            img.Visibility = Visibility.Collapsed;
        }

        void canP_MouseEnter(object sender, MouseEventArgs e)
        {
            Canvas can = sender as Canvas;
            Image img = can.Children[1] as Image;
            img.Visibility = Visibility.Visible;
        }

        void pagelistuc_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            PageListUC uc = sender as PageListUC;
            pluc = uc;
            App.cpage = ((PageListUC)sender).cfyp;
            MainWindow mw = win as MainWindow;
            mw.InitNewPage(App.cpage.PageName);
            // App.pageName = 
            // //打开页面
            // PageTools paget = new PageTools();
            // paget.OpenPage();
            // //删除工作区的Canvas
            //MainWindow.workmainuc.DelCanvas();
            // //新加载一个Canvas
            // Canvas thiscan = CreateCanvas();
            // pt.InitTheWork(App.dt, thiscan);
            // canvas = thiscan;
            // WorkAreaBorder.Child = canvas;
            // if (App.cpage.Width != "" && App.cpage.Height != "")
            // {
            //     canvas.Width = double.Parse(App.cpage.Width);
            //     canvas.Height= double.Parse(App.cpage.Height);
            // }
            // bodySlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(bodySlider_ValueChanged);
            // bodySlider.Value = 0.5;
            // winId.Text = App.cpage.PageName;
            // winWidth.Text = App.cpage.Width;
            // winHeight.Text = App.cpage.Height;
            // pageloadtxt.Text = App.cpage.OnloadInit;
            // winPBorder.Visibility = Visibility.Visible;
            // // uc.mirro.Visual = canvas;
            // BindingTreeView(canvas);
            if (SaveTheTempList())
            {
                pluc.GetBG();
            }
            //  pluc.setSize(App.proWidth,App.proHeight);
        }

        /// <summary>
        /// 建立图片
        /// </summary>
        /// <param name="ui"></param>
        /// <param name="sPath"></param>
        /// <param name="fileName"></param>
        public static bool SaveToImage(FrameworkElement ui)
        {
            App.MiniList.Clear();
            try
            {
                System.IO.MemoryStream fs = new MemoryStream();
                RenderTargetBitmap bmp = new RenderTargetBitmap((int)ui.ActualWidth, (int)ui.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
                bmp.Render(ui);
                BitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bmp));
                encoder.Save(fs);
                byte[] bmpBytes = fs.ToArray();
                fs.Close();
                ImageSource imgSource = ByteArrayToBitmapImage(bmpBytes);
                Object[] obj = { imgSource, App.pageName };
                App.MiniList.Add(obj);
                return true;
            }
            catch
            {
                return false;
            }
        }
        //保存工作区图片
        public static bool SaveTheTempList()
        {
            if (SaveToImage(WorkMainAreaUC.workcanvas))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// byte[]转换bitmapImage
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        public static BitmapImage ByteArrayToBitmapImage(byte[] byteArray)
        {
            BitmapImage bmp = null;

            try
            {
                bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.StreamSource = new MemoryStream(byteArray);
                bmp.EndInit();
            }
            catch
            {
                bmp = null;
            }

            return bmp;
        }

        /// <summary>
        /// 调换控件位置
        /// </summary>
        /// <param name="dragObj">拖拽对象</param>
        /// <param name="dragEnterObj">要拖入的对象</param>
        /// <returns></returns>
        public bool onDrogObj(UIElement dragObj, UIElement dragEnterObj)
        {

            return false;
        }

        //treeview 闭合
        void item_Collapsed(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < treelist.Count; i++)
            {
                if ((treelist[i])[0] == ((TreeViewItem)sender).Name)
                {
                    (treelist[i])[1] = "false";
                }
            }
        }
        //treeview 打开
        void item_Expanded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < treelist.Count; i++)
            {
                if ((treelist[i])[0] == ((TreeViewItem)sender).Name)
                {
                    (treelist[i])[1] = "true";
                }
            }
        }

        void item_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {             
            TreeViewItem tvi = sender as TreeViewItem;
            
            ObjectTreeViewUC ob = tvi.Header as ObjectTreeViewUC;
            TreeViewItem item = sender as TreeViewItem;
            UIElement uie =ob.uie= item.Tag as UIElement;
            if (item.Tag.ToString() == "page")
            {
               // DelAllSel();
                //删除所有绑定
                WorkMainAreaUC.layer = AdornerLayer.GetAdornerLayer(WorkMainAreaUC.workcanvas);
                if (WorkMainAreaUC.myCan != null)
                {
                    WorkMainAreaUC.layer.Remove(WorkMainAreaUC.myCan);
                }
                MainWindow.workrightuc.DelBindingAll();
            }
            else if (item.Tag.GetType() == typeof(Image))
            {
                MainWindow.workrightuc.DelBindingAll();
                App.uiobj = item.Tag as Image;
                if (WorkMainAreaUC.divImg != null)
                {
                    WorkMainAreaUC.divImg.Visibility = Visibility.Visible;
                    WorkMainAreaUC.divImg = null;
                }
                MainWindow.workrightuc.ShowPBorder("image");
                MainWindow.workrightuc.BindingImg(item.Tag as Image); 
            
                ModelTools.listUI.Clear();
                ModelTools.listUI.Add(App.uiobj as UIElement);
            }
            else if (item.Tag.GetType() == typeof(Canvas))
            {
                MainWindow.workrightuc.DelBindingAll();
                App.uiobj = item.Tag as Canvas;
                WorkMainAreaUC.divImg = (Image)(item.Tag as Canvas).Tag;
                WorkMainAreaUC.divImg.Visibility = Visibility.Collapsed;
                MainWindow.workrightuc.ShowPBorder("div");
                MainWindow.workrightuc.BindingDiv(item.Tag as Canvas);

                ModelTools.listUI.Clear();
                ModelTools.listUI.Add(App.uiobj as UIElement);
              
            }
            else if (item.Tag.GetType() == typeof(Rectangle))
            {
                MainWindow.workrightuc.DelBindingAll();
                App.uiobj = item.Tag as Rectangle;
                if (WorkMainAreaUC.divImg != null)
                {
                    WorkMainAreaUC.divImg.Visibility = Visibility.Visible;
                    WorkMainAreaUC.divImg = null;
                }
                MainWindow.workrightuc.ShowPBorder("rect");
                MainWindow.workrightuc.BindingRect(item.Tag as Rectangle);

                ModelTools.listUI.Clear();
                ModelTools.listUI.Add(App.uiobj as UIElement);
            }
            else if (item.Tag.GetType() == typeof(TextBlock))
            {
                MainWindow.workrightuc.DelBindingAll();
                App.uiobj = item.Tag as TextBlock;
                if (WorkMainAreaUC.divImg != null)
                {
                    WorkMainAreaUC.divImg.Visibility = Visibility.Visible;
                    WorkMainAreaUC.divImg = null;
                }
                MainWindow.workrightuc.ShowPBorder("text");
                MainWindow.workrightuc.BindingText(item.Tag as TextBlock);

                ModelTools.listUI.Clear();
                ModelTools.listUI.Add(App.uiobj as UIElement);
            }
            
        }

        private void treeView1_Selected(object sender, RoutedEventArgs e)
        {
            TreeViewItem tvi = sender as TreeViewItem;
            tvi.Opacity = 0.2;
        }

        private void treeView1_Unselected(object sender, RoutedEventArgs e)
        {
            TreeViewItem tvi = sender as TreeViewItem;
            tvi.Opacity = 1;
        }
    }
}
